package br.inatel.ec203.projeto.view;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.event.InternalFrameEvent;

import br.inatel.ec203.projeto.control.ArmarioDAO;
import br.inatel.ec203.projeto.model.Item;

public class EditDeleteFrame extends JFrame{

	private static final long serialVersionUID = -6937807567617228038L;
	
	private ArmarioDAO armarioDAO = new ArmarioDAO();
	
	private SearchPanel searchPanel;
	
	private EditDeleteCancelButtonsPanel editDeleteCancelButtonsPanel;
	
	public EditDeleteFrame() {
		super ("Tela para Editar ou Deletar um componente");
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		initialize();
		
		setSize(new Dimension(500, 600));
		setResizable(false);
		setLocationRelativeTo(null);
	}

	private void initialize() {
		add(getSearchPanel(), BorderLayout.PAGE_START);
	}

	private EditDeleteCancelButtonsPanel getEditDeleteCancelButtonsPanel() {
		if (editDeleteCancelButtonsPanel == null) {
			editDeleteCancelButtonsPanel = new EditDeleteCancelButtonsPanel();
			editDeleteCancelButtonsPanel.addEditDeleteCancelButtonsListener(new EditDeleteCancelButtonsListener() {
				
				@Override
				public void editPerformed() {
					editClicked();
				}
				
				@Override
				public void deletePerformed() {
					deleteClicked();
				}
				
				@Override
				public void cancelPerformed() {
					dispose();					
				}
			});
		}
		return editDeleteCancelButtonsPanel;
	}
	
	public void editClicked() {	
		Item component = findComponent();
		callEditFrame(component);
	}
	
	public void deleteClicked() {
		DeleteOptionPane deleteOptionPane = new DeleteOptionPane();	
		
		Item component = findComponent();		
		if (component != null) {
			String message = "Deseja remover o componente:\n";
			message += component.toStringToPrint();					
			
			@SuppressWarnings("static-access")
			int option = deleteOptionPane.showConfirmDialog(null, message, "Atencao", JOptionPane.YES_NO_OPTION);
			if (option == JOptionPane.YES_NO_OPTION) {			
				armarioDAO.getComponentes().remove(component);
				JOptionPane.showMessageDialog(null, "Componente excluido");
				deleteOptionPane.addDeleteOptionPaneListener(new DeleteOptionPaneListener() {
					@Override
					public void internalFrameClosed(InternalFrameEvent e) {
						getSearchPanel().fillTableBySearch();
					}
				});
				deleteOptionPane.getDeleteOptionPaneListener().internalFrameClosed(null);
			}
		}
	}
	
	public Item findComponent() {
		Item component = null;
		try {
			int rowNum = getSearchPanel().getComponentTable().getSelectedRow();
			String value = (String) getSearchPanel().getComponentTable().getValueAt(rowNum, 1);
			String partNumber = (String) getSearchPanel().getComponentTable().getValueAt(rowNum, 2);
			component = Util.findComponentInArrayList(value, partNumber);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Selecionar Componente", "Erro", JOptionPane.ERROR_MESSAGE);
		}		
		return component;
	}
	
	private void callEditFrame(Item component) {
		if (component != null) {
			EditFrame editFrame = new EditFrame(component);
			editFrame.setTextFields();
			editFrame.addEditFrameListener(new EditFrameListeners() {
				@Override
				public void internalFrameClosed(InternalFrameEvent e) {
					getSearchPanel().fillTableBySearch();
				}
			});
			editFrame.setVisible(true);
		}
	}
	
	public SearchPanel getSearchPanel() {
		if (searchPanel == null) {
			searchPanel = new SearchPanel();
			searchPanel.addSearchPanelListeners(new SearchPanelListeners() {
				
				@Override
				public void searchButtonOfAmountPerformed() {
					searchPanel.searchByComponent();
				}
				
				@Override
				public void partNumberRadioButtonPerformed() {
					searchPanel.remove(searchPanel.getValueSearchPanel());
					searchPanel.remove(searchPanel.getComponentScrollPane());
					remove(getEditDeleteCancelButtonsPanel());
					
					searchPanel.add(searchPanel.getPartNumberSearchPanel(), searchPanel.getPartNumberSearchPanelConstraints());
					searchPanel.add(searchPanel.getComponentScrollPane(), searchPanel.getComponentScrollPaneConstraints());
					add(getEditDeleteCancelButtonsPanel(), BorderLayout.PAGE_END);
					
					searchPanel.fillTable();
					repaint();
					setVisible(isDisplayable());
				}
				
				@Override
				public void componentRadioButtonPerformed() {
					searchPanel.remove(searchPanel.getPartNumberSearchPanel());
					searchPanel.remove(searchPanel.getComponentScrollPane());
					remove(getEditDeleteCancelButtonsPanel());
					
					searchPanel.add(searchPanel.getValueSearchPanel(),searchPanel.getAmountSearchPanelConstraints());
					searchPanel.add(searchPanel.getComponentScrollPane(), searchPanel.getComponentScrollPaneConstraints());
					add(getEditDeleteCancelButtonsPanel(), BorderLayout.PAGE_END);
					
					searchPanel.fillTable();
					repaint();
					setVisible(isDisplayable());
				}

				@Override
				public void searchButtonOfPartNumberPerformed() {
					searchPanel.searchByPartNumber();
				}
			});
		}
		return searchPanel;
	}

}










